<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
    public $enableCsrfValidation = false;

    /*
     * 上传图片  文章里里面
     */
    public function actionFile()
    {

        $data = [
            'code' => 0,
            'msg' => '',
            'data' => [
                'src' => '',
                'title' => ''
            ]
        ];

        $file = $_FILES['file'];
        $upload = 'upload/' . date('Y-m-d') . '/' . date('H') . '/';
        if (!file_exists($upload)) {
            @mkdir($upload, 0777, true);
        }
        $maxSize = 10;
        if (empty($file)) {
            return false;
            //未上传图片
        }
        $foramt = array('jpg', 'jpeg', 'png', 'gif');

// 			$pathinfo = pathinfo($file['name']);
        $pathinfo = pathinfo($file['name']);
        $size = $file['size'];
        if (!in_array(strtolower($pathinfo['extension']), $foramt)) {
            return false;
        }
        if ($size / 1024 / 1024 >= $maxSize) {
            return false;
        }
        $name_img = date('YmdHis') . uniqid() . rand(100000, 999999) . '.' . $pathinfo['extension'];
        if (move_uploaded_file($file['tmp_name'], $upload . $name_img)) {
//              return implode(',',$imglist);
            $data['data']['src'] = config('params.url') . $upload . $name_img;
            return json_encode($data);
        } else {
            return false;
        }

    }


    /*
    * 上传图片  单个大头图
    */
    public function actionPic()
    {
        $data = [
            'code' => 0,
            'msg' => '',
            'data' => [
                'src' => '',
                'title' => ''
            ]
        ];

        $file = $_FILES['file'];
        $upload = 'uploadpic/' . date('Y-m-d') . '/' . date('H') . '/';
        if (!file_exists($upload)) {
            @mkdir($upload, 0777, true);
        }
        $maxSize = 10;
        if (empty($file)) {
            return false;
            //未上传图片
        }
        $foramt = array('jpg', 'jpeg', 'png', 'gif');

// 			$pathinfo = pathinfo($file['name']);
        $pathinfo = pathinfo($file['name']);
        $size = $file['size'];
        if (!in_array(strtolower($pathinfo['extension']), $foramt)) {
            return false;
        }
        if ($size / 1024 / 1024 >= $maxSize) {
            return false;
        }
        $name_img = date('YmdHis') . uniqid() . rand(100000, 999999) . '.' . $pathinfo['extension'];
        if (move_uploaded_file($file['tmp_name'], $upload . $name_img)) {
//              return implode(',',$imglist);
            $data['data']['src'] = config('params.url') . $upload . $name_img;
            return json_encode($data);
        } else {
            return false;
        }

    }

}